<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('项目类型')" prop="dealType">
            <dict-select
              v-model="form.dealType"
              dictionary="deal_type"
              :placeholder="$t('项目类型')"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <br>
      <div class="area-header">
        <span class="area-header-title">{{ $t('评级指标') }}</span>
      </div>
      <el-table
        :data="form.dealConfigRatingIndexList"
        border
        class="nowrapTable formTable"
      >
        <el-table-column :label="$t('序号')" align="center" width="50">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('指标名称')" align="center">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('指标名称') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'dealConfigRatingIndexList.' + scope.$index + '.indexName'" :rules="indexRules.indexName">
              <el-input v-model="scope.row.indexName" :placeholder="$t('指标名称')" show-word-limit maxlength="100" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('操作')" align="center" width="80">
          <template slot-scope="scope">
            <el-button v-hasPermi="['deal:configrating:remove']" size="small" type="text" icon="el-icon-delete" @click="delIndex(scope.$index)" />
          </template>
        </el-table-column>
      </el-table>
      <div class="tol">
        <el-button v-hasPermi="['deal:configrating:edit']" type="primary" icon="el-icon-plus" size="mini" @click="addIndex">{{ $t("添加") }}</el-button>
      </div>
      <br>
      <div class="area-header">
        <span class="area-header-title">{{ $t('评级标准') }}</span>
      </div>

      <el-table
        :data="form.dealConfigRatingStandardList"
        border
        class="nowrapTable formTable"
      >
        <el-table-column :label="$t('评级结果')" align="center" prop="ratingType" :formatter="ratingTypeFormat" width="150" />
        <el-table-column :label="$t('评级标准')" align="center">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('评级标准') }}</span>
          </template>
          <template slot-scope="scope">
            <div>
              <div v-for="(value,index) in scope.row.children" :key="index">
                <el-form-item :prop="'dealConfigRatingStandardList.' + scope.$index + '.children.' +index+ '.ratingStandard'" :rules="standardRules.ratingStandard">
                  <el-input v-model="value.ratingStandard" style="width: 97%;" type="textarea" :placeholder="$t('评级标准')" maxlength="2000" :autosize="{ minRows: 2}" show-word-limit />
                  <el-button
                    v-hasPermi="['deal:configrating:remove']"
                    style="width: 3%;"
                    size="small"
                    type="text"
                    icon="el-icon-delete"
                    @click="delStandard(value, index, scope.$index)"
                  />
                </el-form-item>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="100">
          <template slot-scope="scope">
            <!--            <div v-if="scope.row.children.length > 0">-->
            <!--              <div v-for="(value,index) in scope.row.children" :key="index" style="height: 100% ">-->
            <!--                <el-button-->
            <!--                  v-hasPermi="['deal:configrating:edit']"-->
            <!--                  size="small"-->
            <!--                  type="text"-->
            <!--                  icon="el-icon-circle-plus"-->
            <!--                  @click="addStandard(scope.row, scope.$index)"-->
            <!--                />-->
            <!--                <el-button-->
            <!--                  v-hasPermi="['deal:configrating:remove']"-->
            <!--                  size="small"-->
            <!--                  type="text"-->
            <!--                  icon="el-icon-delete"-->
            <!--                  @click="delStandard(value, index, scope.$index)"-->
            <!--                />-->
            <!--              </div>-->
            <!--            </div>-->
            <el-button
              v-hasPermi="['deal:configrating:edit']"
              size="small"
              type="text"
              icon="el-icon-circle-plus"
              @click="addStandard(scope.row, scope.$index)"
            />
          </template>
        </el-table-column>
      </el-table>

    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="deal:configrating" />
    </div>
  </div>
</template>

<script>
    import { saveDealConfigRating, getDealConfigRating, addNullStandardList } from '@/api/deal/configrating/dealConfigRating'
    export default {
        data() {
            return {
                // 表单参数
                form: {
                  // 评级指标
                  dealConfigRatingIndexList: [],
                  // 评级标准
                  dealConfigRatingStandardList: []
                },
                // 遮罩层
                loading: false,
              // 评级结果字典
              ratingTypeOptions: [],
                // 表单校验
                rules: {
                  dealType: [
                    { required: true, message: this.$t('必填信息'), trigger: 'change' }
                  ]
                },
              indexRules: {
                indexName: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
              },
              standardRules: {
                ratingStandard: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
              }
            }
        },
        created() {
            this.getDicts('deal_type').then(response => {
                this.dealTypeOptions = response.data
            })
          this.getDicts('current_rating_type').then(response => {
            this.ratingTypeOptions = response.data
          })
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getDealConfigRating(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    }).catch(() => {
                      this.loading = false
                    })
                } else {
                    this.reset()
                    // 获取标准字典
                    this.addNullStandardList()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    dealType: undefined,
                    configName: undefined,
                    remark: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined,
                    delFlag: undefined,
                  // 评级指标
                  dealConfigRatingIndexList: [],
                  // 评级标准
                  dealConfigRatingStandardList: []
                }
                this.resetForm('form')
            },
          addNullStandardList() {
            addNullStandardList().then(response => {
              console.log(response, 'response')
              this.form.dealConfigRatingStandardList = response.data
              console.log(this.form, 'this.formthis.form')
            })
          },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
          // 评级结果字典翻译
          ratingTypeFormat(row) {
            return this.selectDictLabel(this.ratingTypeOptions, row.ratingType)
          },
            /** 提交按钮 */
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveDealConfigRating(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                }).catch(() => {
                    this.loading = false
                })
           },
          // 添加评级指标
          addIndex() {
            const row = {
              id: undefined,
              ratingId: undefined,
              indexName: undefined,
              remark: undefined
            }
            if (!(this.form.dealConfigRatingIndexList instanceof Array)) {
              this.$set(this.form, 'dealConfigRatingIndexList', [])
            }
            this.$nextTick(() => {
              this.form.dealConfigRatingIndexList.push(row)
            })
          },
          // 删除评级指标
          delIndex(index) {
            this.form.dealConfigRatingIndexList.splice(index, 1)
          },
          // 添加评级标准
          addStandard(row, index) {
              console.log(row, index, 'row, index')
            const child = {
              id: undefined,
              ratingId: undefined,
              ratingType: row.ratingType,
              parentLevel: false,
              ratingStandard: undefined
            }
            if (!(this.form.dealConfigRatingStandardList[index].children instanceof Array)) {
              this.$set(this.form.dealConfigRatingStandardList[index], 'children', [])
            }
            this.$nextTick(() => {
              this.form.dealConfigRatingStandardList[index].children.push(child)
            })
          },
          // 删除评级标准
          delStandard(row, index, zindex) {
              console.log(row, index, 'row, index')
            this.form.dealConfigRatingStandardList[zindex].children.splice(index, 1)
          }

        }
    }
</script>
<style lang="scss" scoped>
  .standardPlan{
    div{
      overflow: hidden;
      text-overflow: ellipsis;
      line-height: 100px;
      border-bottom: 1px solid #dfe6ec!important;
      padding: 0 10px;
      &:last-child{
        border-bottom: 0;
      }
    }
  }
</style>

